<?php
    require_once ( "../config.php" );
    require_once ( "./check_data.php" );
    
    
    //检查同一ip访问次数
    if ($check_ip_enable) {
        $ip = get_ip();
        $m = new_memcached();
        $objip = $m->get_obj()->get($ip);
        if ($objip!=null && $objip!=FALSE) {
            $error = new my_error("1 times per second");
            exit( $error->to_string() );
        }
        $m->get_obj()->set($ip, $ip, 1);
    }
    
    $type = null;
    $data = null;
    if ($_REQUEST!=null && isset($_REQUEST["data"]) && is_string($_REQUEST["data"]) ) {
        $data = str_check(strval($_REQUEST["data"]));
    }
    if ($data === null || $data==="" ) {
        $error = new my_error("param_data_error");
        exit( $error->to_string() );
    }
    
    if ($_REQUEST!=null && isset($_REQUEST["type"]) && is_string($_REQUEST["type"]) ) {
        $type = verify_id($_REQUEST["type"]);
    }
    if ($type === null || $type===0 ) {
        $error = new my_error("param_type_error");
        exit( $error->to_string() );
    }
    
    if (my_check_data($type, $data)==FALSE) {
        $error = new my_error("param_check_data_failed_error");
        exit( $error->to_string() );
    }
    
    $small = null;
    if ($_REQUEST!=null && isset($_REQUEST["small"]) && is_string($_REQUEST["small"]) ) {
        $small = str_check(strval($_REQUEST["small"]));
    }
    
    $res_dir = sys_get_temp_dir()."/";
    $id = null;
    while(TRUE) {
        $id = get_key();
        if (file_exists($res_dir.$id)==FALSE) {
            break;
        }
    }
    
    $filename = $res_dir.$id;
    $f = fopen($filename, "wb");
    if ($f==null || $f==FALSE) {
        $error = new my_error("create_file_failed#1");
        exit( $error->to_string() );
    }
    fwrite($f, $data);
    fclose($f);
    
    $ftp = new ftp($ftp_host, $ftp_port, $ftp_user, $ftp_pass);
    if ($ftp->connect()==FALSE) {
        $error = new my_error("connect_ftp_failed#1!");
        exit( $error->to_string() );
    }
    if ($ftp->up_file($filename, $id, FALSE)==FALSE) {
        $error = new my_error("upload_file_failed#1!");
        exit( $error->to_string() );
    }
    $ftp->close();
    
    //small
    if ($small && $small!="") {
        $filename2 = $res_dir.$id."s";
        $f2 = fopen($filename2, "wb");
        if ($f2==null || $f2==FALSE) {
            $error = new my_error("create_file_failed#2");
            exit( $error->to_string() );
        }
        fwrite($f2, $small);
        fclose($f2);
    
        $ftp2 = new ftp($ftp_host, $ftp_port, $ftp_user, $ftp_pass);
        if ($ftp2->connect()==FALSE) {
            $error = new my_error("connect_ftp_failed#2!");
            exit( $error->to_string() );
        }
        if ($ftp2->up_file($filename2, $id."s", FALSE)==FALSE) {
            $error = new my_error("upload_file_failed#2!");
            exit( $error->to_string() );
        }
        $ftp2->close();
    }
    
    $retval = new my_retval($id);
    exit($retval->to_string());

